package com.hw.paltfrom.service.comper;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hw.paltfrom.mapper.HwOaWechatpicMapper;
import com.hw.paltfrom.mapper.HwbaseDataItemDetailMapper;
import com.hw.paltfrom.mapper.HwbaseDataItemMapper;
import com.hw.paltfrom.pojo.HwBaseDataItem;
import com.hw.paltfrom.pojo.HwBaseDataItemDetail;
import com.hw.paltfrom.pojo.HwOaWechatpic;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.OaWechatpicVo;


/**
 *微信小程序广告位
 * 李俊君
 * 2019年11月6日15:06:04
 */
@Service
public class OaWechatpicServer {
	@Autowired
	private HwOaWechatpicMapper oaWechatpicMapper;
    @Autowired
    private HwbaseDataItemMapper dataItemMapper;
    @Autowired
    private HwbaseDataItemDetailMapper dataItemDetailMapper;
	

	public Map<String, Object> insert(OaWechatpicVo vo) {
		// 先判断分类名称
		

		// 生成ID、创建时间、插入数据库
		HwOaWechatpic hwOaWechatpic = new HwOaWechatpic();
		hwOaWechatpic.setfId(RandomNumberUtil.getNumber());
		hwOaWechatpic.setfWechatcode(vo.getfWechatcode());
		hwOaWechatpic.setfWechatname(vo.getfWechatname());
		hwOaWechatpic.setfEnablemark(vo.getfEnablemark());
		hwOaWechatpic.setfPicpath(vo.getfPicpath());
		hwOaWechatpic.setfUrlpath(vo.getfUrlpath());
		hwOaWechatpic.setfDescription(vo.getfDescription());
		hwOaWechatpic.setfCreatedate(new Timestamp(System.currentTimeMillis()));
		hwOaWechatpic.setfCreateuserid(vo.getfCreateuserid());
		hwOaWechatpic.setfCreateusername(vo.getfCreateusername());

		Integer insert = oaWechatpicMapper.insert(hwOaWechatpic);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);

	}

	// 查询
	public Map<String, Object> queryAllList(OaWechatpicVo vo) {
		Wrapper<HwOaWechatpic> namewrapper = new EntityWrapper<HwOaWechatpic>();
		if (StringUtils.isNotBlank(vo.getfWechatcode())) {
			namewrapper.eq("F_WeChatCode", vo.getfWechatcode());
		}
		if (StringUtils.isNotBlank(vo.getfWechatname())) {
			namewrapper.eq("F_WeChatName", vo.getfWechatname());
		}
		namewrapper.orderBy("F_CreateDate",false);
		List<HwOaWechatpic> oaWechatpicsList = oaWechatpicMapper.selectList(namewrapper);
		JSONArray array = JSONArray.parseArray(JSON.toJSONString(oaWechatpicsList));
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, array);
	}

	// 更新
	public Map<String, Object> update(OaWechatpicVo vo) {
		// 通过ID查询待更新的数据
		Wrapper<HwOaWechatpic> wrapper = new EntityWrapper<HwOaWechatpic>();
		wrapper.eq("F_Id", vo.getfId());
		HwOaWechatpic hwOaWechatpic = new HwOaWechatpic();
		// 记录是谁更新
		hwOaWechatpic.setfWechatname(vo.getfWechatname());
		hwOaWechatpic.setfEnablemark(vo.getfEnablemark());
		hwOaWechatpic.setfPicpath(vo.getfPicpath());
		hwOaWechatpic.setfUrlpath(vo.getfUrlpath());
		hwOaWechatpic.setfDescription(vo.getfDescription());
		hwOaWechatpic.setfModifydate(new Timestamp(System.currentTimeMillis()));
		hwOaWechatpic.setfModifyuserid(vo.getfModifyuserid());
		hwOaWechatpic.setfModifyusername(vo.getfModifyusername());
		Integer update = oaWechatpicMapper.update(hwOaWechatpic, wrapper);

		if (update != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}

		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	public Map<String, Object> delete(OaWechatpicVo vo) {
		// 通过ID查询待删除的数据
		Wrapper<HwOaWechatpic> wrapper = new EntityWrapper<HwOaWechatpic>();
		wrapper.eq("F_Id", vo.getfId());
		Integer delete = oaWechatpicMapper.delete(wrapper);
		if (delete != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}

		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}
	
    //查询微信广告位
    public Map<String, Object> findTypes() {
        //查询微信广告位字典类型id
        Wrapper<HwBaseDataItem> dataItemWrapper = new EntityWrapper<>();
        dataItemWrapper.eq("F_ItemName","微信广告位");
        List<HwBaseDataItem> hwBaseDataItems = dataItemMapper.selectList(dataItemWrapper);
        if (hwBaseDataItems.size() == 0){
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        HwBaseDataItem hwBaseDataItem = hwBaseDataItems.get(0);
        String itemId = hwBaseDataItem.getfItemid();//微信广告位字典类型id
        //根据字典分类id查询分类详情名称
        Wrapper<HwBaseDataItemDetail> dataItemDetailWrapper =  new EntityWrapper<>();
        dataItemDetailWrapper.eq("F_ItemId",itemId).orderBy("F_SortCode");
        List<HwBaseDataItemDetail> hwBaseDataItemDetails = dataItemDetailMapper.selectList(dataItemDetailWrapper);

        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,hwBaseDataItemDetails);
    }

}
